<template>
  <div>
    <base-right-dialog
      ref="baseRightDialog"
      :footerShow="true"
      :dialogVisible.sync="dialogVisible"
      :title="dialogTitle + '用户'"
      :type="dialogType"
      @handleClose="handleDialogClose"
      @handleConfirmClick="handleConfirmClick"
      :size="'820px'"
      :submitShow="submitShow"
    >
      <div class="dialogList">
        <base-form
          ref="basicsForm"
          :formRow="formRow"
          :isFunBtn="false"
          :rules="basicsRules"
          class="dialog_form"
          :spanWidth="`120px`"
          :loading="vLoading"
          style="padding-bottom: 30px"
          @elDialogClick="elDialogClick"
        >
        </base-form>
      </div>
    </base-right-dialog>
  </div>
</template>

<script>
import baseRightDialog from "@/components/base/baseRightDialog";
import baseForm from "@/components/base/baseNewForm";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseRightDialog,
    baseForm,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      dialogType: "",
      formRow: [],
      basicsRules: configData.basicsRules,
      vLoading: false,
      submitShow: true,
    };
  },
  methods: {
    openDialog(type, row) {
      this.dialogVisible = true;
      this.submitShow = true;
      // 新增
      if (type == "add") {
        this.dialogTitle = "新增";
        this.formRow = configData.formRow;
        this.dialogType = "add";
        this.$nextTick(()=>{
          this.$refs.basicsForm.getField("state", "0");
        })
      }
      // 编辑
      if (type == "edit") {
        this.dialogTitle = "编辑";
        this.formRow = configData.formRow1;
        this.dialogType = "edit";
        this.userGetById(row.id);
      }
      //查看
      if (type == "show") {
        this.dialogTitle = "查看";
        this.formRow = configData.formRowShow;
        this.dialogType = "show";
        this.submitShow = false;
        this.userGetById(row.id);
      }
    },
    // 编辑 详情
    async userGetById(id) {
      let param = {
        id: id,
      };
      let res = await authApi(
        "userService",
        "user",
        "queryEntity",
        "",
        param
      );
      if (res.status == "200") {
        this.$nextTick(() => {
          this.$refs.basicsForm.incomingParameters(res.attribute);
        });
      }
    },
    // 弹窗关闭
    handleDialogClose() {
      this.$refs.basicsForm.resetFields();
      this.dialogVisible = false;
    },
    // 弹窗确认按钮
    handleConfirmClick() {
      this.$refs.basicsForm.$refs["ruleForm"].validate((valid) => {
        if (!valid) {
          return;
        } else {
          let params = {
            ...this.$refs.basicsForm.ruleForm,
          };
          if (this.dialogType == "add") {
            this.openLoading("submit")
            this.userEntitySaveDto(params);
          }
          if (this.dialogType == "edit") {
            this.openLoading("submit")
            this.userEntityUpdateDto(params);
          }
        }
      });
    },
    // 新增保存
    async userEntitySaveDto(params) {
      let res = await authApi(
        "userService",
        "user",
        "addEntity",
        "",
        params
      );
      if (res.status == "200") {
        this.$nextTick(() => {
          this.handleDialogClose();
          this.$vmNews("新增成功", "success");
          this.$emit("resetTable");
        });
      }
    },
    // 编辑保存
    async userEntityUpdateDto(params) {
      let res = await authApi(
        "userService",
        "user",
        "updateEntity",
        "",
        params
      );
      if (res.status == "200") {
        this.$nextTick(() => {
          this.handleDialogClose();
          this.$vmNews("更新成功", "success");
          this.$emit("resetTable");
        });
      }
    },
    // 弹窗——打开弹窗
    elDialogClick(row, index, indexRow) {
      if (row.label == "NC仓库编码") {
        this.$refs.ncWarehouse.openDialog();
      }
    },
  },
};
</script>

<style scoped>
::v-deep .el-form-item__content .el-checkbox-group {
  display: flex;
  flex-direction: column;
}
</style>